<template>
  <div class="home">
    <!-- 导航栏 -->
    <el-header class="header">
      <div class="container flex-between">
        <div class="logo">
          <h2>博客系统</h2>
        </div>
        <div class="nav-menu">
          <div class="nav-content">
            <!-- 导航链接 -->
            <div class="nav-links">
              <a
                href="#"
                @click.prevent="handleMenuSelect('home')"
                :class="{ active: activeIndex === 'home' }"
                class="nav-link"
              >
                首页
              </a>
              <a
                v-if="!authStore.isLoggedIn"
                href="#"
                @click.prevent="handleMenuSelect('login')"
                :class="{ active: activeIndex === 'login' }"
                class="nav-link"
              >
                登录
              </a>
              <a
                v-if="!authStore.isLoggedIn"
                href="#"
                @click.prevent="handleMenuSelect('register')"
                :class="{ active: activeIndex === 'register' }"
                class="nav-link"
              >
                注册
              </a>
            </div>

            <!-- 用户信息区域 -->
            <div v-if="authStore.isLoggedIn" class="user-info-section">
              <div class="user-profile">
                <el-avatar :size="32" :src="authStore.user?.avatar">
                  {{ authStore.user?.nickname || authStore.user?.username }}
                </el-avatar>
                <span class="username">{{ authStore.user?.nickname || authStore.user?.username }}</span>
              </div>
              <div class="user-actions">
                <el-button size="small" @click="handleMenuSelect('admin')">
                  管理后台
                </el-button>
                <el-button size="small" type="danger" @click="handleMenuSelect('logout')">
                  退出登录
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-header>

    <!-- 主要内容 -->
    <el-main class="main-content">
      <!-- 欢迎区域 -->
      <div class="hero-section">
        <div class="container">
          <div class="hero-content">
            <h1>欢迎来到博客系统</h1>
            <p>分享知识，记录生活，连接世界</p>
            <div class="hero-actions">
              <el-button v-if="!authStore.isLoggedIn" size="large" @click="$router.push('/login')">
                开始写作
              </el-button>
              <el-button v-else size="large" @click="$router.push('/admin/articles/create')">
                开始写作
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <div class="container">
        <!-- 搜索栏 -->
        <div class="search-section">
          <div class="search-bar">
            <el-input
              v-model="searchKeyword"
              placeholder="搜索文章..."
              size="large"
              clearable
              @keyup.enter="handleSearch"
            >
              <template #append>
                <el-button @click="handleSearch">
                  <el-icon><Search /></el-icon>
                </el-button>
              </template>
            </el-input>
          </div>
        </div>

        <!-- 文章列表 -->
        <div class="articles-section">
          <h2>{{ searchKeyword ? '搜索结果' : '最新文章' }}</h2>
          <div class="articles-container" v-loading="articleStore.loading">
            <div class="articles-list">
              <div
                v-for="article in articleStore.articles"
                :key="article.id"
                class="article-item"
                @click="$router.push(`/articles/${article.id}`)"
              >
                <div class="article-cover" v-if="article.cover_image">
                  <img :src="article.cover_image" :alt="article.title" />
                </div>
                <div class="article-content">
                  <h3 class="article-title">{{ article.title }}</h3>
                  <p class="article-summary">{{ article.summary || '暂无摘要' }}</p>
                  <div class="article-meta">
                    <span class="author">
                      <el-icon><User /></el-icon>
                      {{ article.author?.nickname || article.author?.username }}
                    </span>
                    <span class="date">
                      <el-icon><Calendar /></el-icon>
                      {{ formatDate(article.created_at) }}
                    </span>
                    <span class="views">
                      <el-icon><View /></el-icon>
                      {{ article.view_count }} 阅读
                    </span>
                    <span class="likes">
                      <el-icon><Star /></el-icon>
                      {{ article.like_count || 0 }} 点赞
                    </span>
                  </div>
                </div>
              </div>
            </div>

            <!-- 空状态 -->
            <div class="empty-state" v-if="!articleStore.loading && articleStore.articles.length === 0">
              <p>{{ searchKeyword ? '没有找到相关文章' : '暂无文章' }}</p>
            </div>

            <!-- 分页 -->
            <div class="pagination" v-if="articleStore.total > 0">
              <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :total="articleStore.total"
                :page-sizes="[10, 20, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </div>
      </div>
    </el-main>

    <!-- 页脚 -->
    <el-footer class="footer">
      <div class="container text-center">
        <p>&copy; 2025 博客系统. All rights reserved.</p>
      </div>
    </el-footer>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useArticleStore } from '@/stores/article'
import {
  Search,
  User,
  Calendar,
  View,
  Star
} from '@element-plus/icons-vue'

const router = useRouter()
const authStore = useAuthStore()
const articleStore = useArticleStore()

const activeIndex = ref('home')
const searchKeyword = ref('')
const currentPage = ref(1)
const pageSize = ref(10)

// 处理菜单选择
const handleMenuSelect = (index: string) => {
  switch (index) {
    case 'home':
      router.push('/')
      break
    case 'login':
      router.push('/login')
      break
    case 'register':
      router.push('/register')
      break
    case 'admin':
      router.push('/admin')
      break
    case 'logout':
      authStore.logout()
      break
  }
}

// 搜索文章
const handleSearch = () => {
  currentPage.value = 1
  fetchArticles()
}

// 分页大小改变
const handleSizeChange = (size: number) => {
  pageSize.value = size
  currentPage.value = 1
  fetchArticles()
}

// 当前页改变
const handleCurrentChange = (page: number) => {
  currentPage.value = page
  fetchArticles()
}

// 获取文章列表
const fetchArticles = async () => {
  try {
    await articleStore.fetchArticles({
      page: currentPage.value,
      page_size: pageSize.value,
      keyword: searchKeyword.value || undefined
    })
  } catch (error) {
    console.error('获取文章列表失败:', error)
  }
}

// 格式化日期
const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleDateString('zh-CN')
}

// 初始化
onMounted(() => {
  fetchArticles()
})
</script>

<style lang="scss" scoped>
.home {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: 64px;

  .container {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .logo h2 {
    color: var(--primary-color);
    margin: 0;
    line-height: 1;
  }

  .nav-menu {
    height: 100%;

    .nav-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      height: 100%;

      .nav-links {
        display: flex;
        align-items: center;
        gap: 30px;
        height: 100%;

        .nav-link {
          color: var(--text-primary);
          text-decoration: none;
          font-size: 16px;
          font-weight: 500;
          padding: 0 16px;
          border-radius: 6px;
          transition: all 0.3s ease;
          position: relative;
          display: flex;
          align-items: center;
          height: 100%;

          &:hover {
            color: var(--primary-color);
            background-color: rgba(102, 126, 234, 0.1);
          }

          &.active {
            color: var(--primary-color);

            &::after {
              content: '';
              position: absolute;
              bottom: 0;
              left: 50%;
              transform: translateX(-50%);
              width: 20px;
              height: 2px;
              background-color: var(--primary-color);
              border-radius: 1px;
            }
          }
        }
      }

      .user-info-section {
        display: flex;
        align-items: center;
        gap: 15px;
        height: 100%;

        .user-profile {
          display: flex;
          align-items: center;
          gap: 8px;

          .username {
            font-weight: 500;
            color: var(--text-primary);
            font-size: 14px;
            line-height: 1;
          }
        }

        .user-actions {
          display: flex;
          align-items: center;
          gap: 8px;

          .el-button {
            padding: 6px 12px;
            font-size: 12px;
            height: auto;
          }
        }
      }
    }
  }
}

.main-content {
  flex: 1;
  padding: 0;
}

.hero-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 80px 0;
  text-align: center;
  
  .hero-content {
    h1 {
      font-size: 3rem;
      margin-bottom: 20px;
      font-weight: 300;
    }
    
    p {
      font-size: 1.2rem;
      margin-bottom: 40px;
      opacity: 0.9;
    }
    
    .hero-actions {
      .el-button {
        margin: 0 10px;
      }
    }
  }
}

.search-section {
  padding: 40px 0 20px 0;

  .search-bar {
    max-width: 1200px;
    margin: 0 auto;
  }
}

.articles-section {
  padding: 20px 0 60px 0;

  h2 {
    text-align: center;
    margin-bottom: 40px;
    color: var(--text-primary);
  }

  .articles-container {
    .articles-list {
      display: flex;
      flex-direction: column;
      gap: 20px;

      .article-item {
        background: white;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        cursor: pointer;
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        display: flex;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
        }

        .article-cover {
          width: 200px;
          height: 150px;
          flex-shrink: 0;
          overflow: hidden;

          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }

        .article-content {
          flex: 1;
          padding: 20px;
          display: flex;
          flex-direction: column;

          .article-title {
            margin: 0 0 10px 0;
            color: var(--text-primary);
            font-size: 1.3rem;
            line-height: 1.4;
            font-weight: 600;
          }

          .article-summary {
            color: var(--text-regular);
            margin-bottom: 15px;
            line-height: 1.6;
            flex: 1;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
          }

          .article-meta {
            display: flex;
            gap: 20px;
            font-size: 0.9rem;
            color: var(--text-secondary);

            span {
              display: flex;
              align-items: center;
              gap: 4px;

              .el-icon {
                font-size: 14px;
              }
            }

            .author {
              font-weight: 500;
            }
          }
        }
      }
    }

    .empty-state {
      text-align: center;
      padding: 60px 20px;
      color: var(--text-secondary);

      p {
        font-size: 1.1rem;
        margin: 0;
      }
    }

    .pagination {
      display: flex;
      justify-content: center;
      margin-top: 40px;
    }
  }
}

.footer {
  background: #001529;
  color: white;
  
  p {
    margin: 0;
    opacity: 0.8;
  }
}

@media (max-width: 768px) {
  .hero-content h1 {
    font-size: 2rem;
  }

  .search-section {
    padding: 30px 0 15px 0;
  }

  .header {
    .nav-menu {
      .nav-content {
        flex-direction: column;
        gap: 15px;
        align-items: stretch;

        .nav-links {
          justify-content: center;
          gap: 20px;

          .nav-link {
            font-size: 14px;
            padding: 6px 12px;
          }
        }

        .user-info-section {
          justify-content: space-between;
          padding: 10px 0;
          border-top: 1px solid #f0f0f0;

          .user-actions {
            .el-button {
              padding: 4px 8px;
              font-size: 11px;
            }
          }
        }
      }
    }
  }

  .articles-section {
    .articles-container {
      .articles-list {
        .article-item {
          flex-direction: column;

          .article-cover {
            width: 100%;
            height: 200px;
          }

          .article-content {
            .article-meta {
              flex-wrap: wrap;
              gap: 10px;
            }
          }
        }
      }
    }
  }
}
</style>
